from docx import Document
from lxml import etree

# 定义WordprocessingML命名空间
NS = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}

def get_list_number(paragraph):
    """获取段落的编号"""
    def find_numId_ilvl(paragraph):
        numPr = etree.ElementTree(paragraph._element).xpath('.//w:numPr', namespaces=NS)
        print('numPr', numPr)
        if numPr and  numPr[0].xpath('.//w:ilvl'):
            ilvl = numPr[0].xpath('.//w:ilvl')[0].get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val')
            numId = numPr[0].xpath('.//w:numId')[0].get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val')
            return numId, ilvl
        return None, None

    numId, ilvl = find_numId_ilvl(paragraph)
    if not numId:
        return ''

    num_elements = etree.ElementTree(paragraph.part.numbering_part.element).xpath('.//w:num', namespaces=NS)
    for num_element in num_elements:
        if num_element.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}numId') == numId:
            abstractNumId = num_element.find('.//w:abstractNumId', namespaces=NS).get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val')
            break
    else:
        return ''

    abstractNum_elements = etree.ElementTree(paragraph.part.numbering_part.element).xpath('.//w:abstractNum', namespaces=NS)
    for abstractNum_element in abstractNum_elements:
        if abstractNum_element.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}abstractNumId') == abstractNumId:
            lvl_elements = abstractNum_element.xpath('.//w:lvl', namespaces=NS)
            for lvl_element in lvl_elements:
                if lvl_element.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}ilvl') == ilvl:
                    start_element = lvl_element.find('.//w:start', namespaces=NS)
                    print('start_element', start_element, start_element.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val'))
                    if start_element is not None:
                        return start_element.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val') + '.'
                    else:
                        return '1.'
    return ''

def convert_auto_numbering_to_plain_text(doc_path):
    doc = Document(doc_path)

    for paragraph in doc.paragraphs:
        if etree.ElementTree(paragraph._element).xpath('.//w:numPr', namespaces=NS):
            # 获取段落的编号
            list_number = get_list_number(paragraph)
            # 获取段落文本
            text = paragraph.text
            # 创建一个新的普通段落
            new_paragraph = paragraph.insert_paragraph_before(f"{list_number} {text}")
            new_paragraph.style = doc.styles['Normal']
            # 删除原始带有编号的段落
            p = paragraph._element
            p.getparent().remove(p)

    # 保存更改后的文档
    new_doc_path = 'new_' + doc_path
    doc.save(new_doc_path)
    print(f"已保存到 {new_doc_path}")

# 示例使用
convert_auto_numbering_to_plain_text('example.docx')
